package com.yanqu.road.server.manger.activity.celebration;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.celebration.CelebrationActivityConfig;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.logic.bussiness.activity.CelebrationBusiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.celebration.CelebrationModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.GameErrorCode;

import java.util.*;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @Description 庆典
 * @Author cwq
 * @Data 2021/7/12 15:26
 */
public class CelebrationMgr extends TempMgr {

    private static ActivityInfo activityInfo;

    private static CelebrationActivityConfig celebrationActivityConfig;

    /**
     * 当前周年庆建筑总等级 （该值由于跨服同步下来）
     */
    private static AtomicLong activityBuildLv = new AtomicLong(-1);

    @Override
    public boolean init() throws Exception {
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean save() {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static void reloadActivityData() {
        getLogger().info("reload celebration activity start");
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.CelebrationActivity.getValue());
        if (openActivityInfoList.size() <= 0) {
            activityInfo = null;
            celebrationActivityConfig = null;
            activityBuildLv.set(-1);
            return;
        }
        activityInfo = openActivityInfoList.get(0);
        // 配置
        CelebrationActivityConfig tempCelebrationActivityConfig = new CelebrationActivityConfig();
        tempCelebrationActivityConfig.setScoreConfigList(CelebrationBusiness.getCelebrationScoreConfigList(activityInfo.getActivityId()));
        tempCelebrationActivityConfig.parseConfig(activityInfo);

        celebrationActivityConfig = tempCelebrationActivityConfig;
        playerReloadActivity();
        getLogger().info("reload celebration activity start");
    }

    public static ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    private static void playerReloadActivity() {
        List<GamePlayer> playerList = GamePlayerMgr.getCopyPlayerList();
        playerList.forEach(action -> {
            action.getModule(CelebrationModule.class).syncConfig();
        });
    }


    /**
     * 判断是否能进入游戏
     *
     * @return
     */
    public static int canEnterActivity(GamePlayer player, int activityId) {
        if (CelebrationMgr.getActivityInfo() == null || CelebrationMgr.getActivityInfo().getActivityId() != activityId) {
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        if (!ActivityMgr.activityInTime(CelebrationMgr.getActivityInfo())) {
            return GameErrorCode.E_CELEBRATION_ACTIVITY_NO_IN_TIME;
        }
        if (!SystemOpenMgr.systemOpen(player, eSystemId.CelebrationActivity.getValue())) {
            return GameErrorCode.E_CELEBRATION_ACTIVITY_NO_UNLOCK;
        }
        return 0;
    }

    public static CelebrationActivityConfig getCelebrationActivityConfig() {
        return celebrationActivityConfig;
    }

    /**
     * 设置 同步周年庆等级
     * @param buildLv
     */
    public static void setActivityBuildLv(long buildLv) {
        if(CelebrationMgr.activityBuildLv.longValue() < buildLv){
            CelebrationMgr.activityBuildLv.set(buildLv);
        }
    }

    public static AtomicLong getActivityBuildLv() {
        return activityBuildLv;
    }
}
